package com.tl.web05.service;


import com.tl.web05.config.Config;
import com.tl.web05.constant.ResponseResult;
import com.tl.web05.dao.UserDao;
import com.tl.web05.model.entity.User;

import javax.servlet.ServletContext;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

public class UserService {
    private UserDao userDao;

    public UserService(UserDao userDao) {
        this.userDao = userDao;
    }

    public UserService() {
    }

    /************************************数据库版**********************************/

    /**
     * 查询所有用户
     *
     * @author Antg
     * @date 2021/7/30 20:18
     */

    public ResponseResult findAllUser() {
        try {
            List<User> userList = userDao.getAllUser();
            if (userList == null || userList.size() == 0) {
                return new ResponseResult<List<User>>("没有用户", 400, null);
            }
            return new ResponseResult<List<User>>("查询成功", 200, userList);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
    }

    /**
     * 根据用户名查询用户
     *
     * @author Antg
     * @date 2021/7/30 20:32
     */
    public ResponseResult findUserByUserName(String username) {
        try {
            User user = userDao.getUserByUserName(username);
            if (user == null) {
                return new ResponseResult("用户不存在", 400, null);
            }
            return new ResponseResult("查询成功", 200, user);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
    }

    /**
     * 删除用户
     *
     * @author Antg
     * @date 2021/7/30 20:26
     */
    public ResponseResult delUser(String username) throws SQLException {
        try {
            boolean flag = userDao.delUser(username);
            if (flag) {
                return new ResponseResult("删除成功", 200, true);
            } else {
                return new ResponseResult("删除失败", 400, false);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
    }

    /**
     * 编辑用户
     *
     * @author Antg
     * @date 2021/7/30 23:09
     */
    public ResponseResult modifyUser(User user) {
        try {
            userDao.modifyUser(user);
            return new ResponseResult("编辑成功", 200, true);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
    }


    /************************************map版**********************************/
    /**
     * 根据用户名查找用户
     *
     * @author Antg
     * @date 2021/7/30 20:26
     */
    public ResponseResult findUserByUserName(HashMap<String, User> map, String username) {
        try {
            User user = map.get(username);
            return new ResponseResult<User>("查询成功", 200, user);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 删除用户
     *
     * @author Antg
     * @date 2021/7/30 20:26
     */
    public ResponseResult delUser(HashMap<String, User> map, String username) {
        try {
            map.remove(username);
            return new ResponseResult<Boolean>("删除成功", 200, true);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 编辑用户
     *
     * @author Antg
     * @date 2021/7/30 20:26
     */
    public ResponseResult modify(ServletContext application, User user) {
        try {
            HashMap<String, User> map = (HashMap<String, User>) application.getAttribute(Config.USER_MAP);
            map.put(user.getUsername(), user);
            return new ResponseResult("编辑成功", 200, true);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
